package com.rongkang.interview.juc.cas;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Description:
 * @author: 荣康
 * @date: 2021/12/2
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
class  User{
    String name;
    int age;
}
public class AtomicReferenceDemo {
    public static void main(String[] args) {
        User z3 = new User("z3",22);
        User li4 = new User("li4",23);
        //创建原子引用
        AtomicReference<User> atomicReference = new AtomicReference<User>(z3);
        System.out.println(atomicReference.compareAndSet(z3, li4)  +"\t " + atomicReference.get());
        System.out.println(atomicReference.compareAndSet(li4, z3)  +"\t " + atomicReference.get());
    }
}
